package org.come.Jpanel;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import org.come.bean.RoleShow;
import org.come.bean.TeamBean;
import org.come.entity.TeamRole;
import org.come.until.FormsManagement;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.TeamPanelBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcp.GetTcpPath;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



public class TeamJpanel extends JPanel {

	private TeamModelJpanel[] modelJpanels;
	private TeamPanelBtn[] operates;// 操作按钮

	private TeamPanelBtn btnOut,// 解散/离开 队伍
			btnTobecaptain,// 设为队长
			btnfiend,// 离开队伍
			btnKickout,// 踢出队伍
			btnApply;// 申请列表
	private int xz = -1;// 选中单位
	private int sxz = -1;// 鼠标滑过单位

	public TeamJpanel() throws Exception {
		if("水墨UI".equals(MyIsif.getStyle())) {

			this.setPreferredSize(new Dimension(538, 279));
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);
			modelJpanels = new TeamModelJpanel[5];
			operates = new TeamPanelBtn[4];
			for (int i = 0; i < modelJpanels.length; i++) {
				modelJpanels[i] = new TeamModelJpanel(this, i);
				modelJpanels[i].setBounds(53 + i * 95, 48, 84, 174);
				this.add(modelJpanels[i]);
			}
			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 13);
			offBtn.setBounds(538 - 37, 10, 25, 25);
			this.add(offBtn);
			// 设为队长
			btnTobecaptain = new TeamPanelBtn("skin/8100226/newbutton/B22.png", 1, "移交队长",
					UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, this, null);
			btnTobecaptain.setBounds(385, 20, 99, 24);
			this.add(btnTobecaptain);
			// 申请列表
			btnApply = new TeamPanelBtn("skin/8100226/newbutton/B22.png", 1, "申请列表",
					UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, this, null);
			btnApply.setBounds(53, 232, 99, 24);
			this.add(btnApply);
			// 解散队伍
			btnOut = new TeamPanelBtn("skin/8100226/newbutton/B22.png", 1, "解散队伍",
					UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, this, null);
			btnOut.setBounds(173, 232, 99, 24);
			this.add(btnOut);
			// 踢出队伍
			btnKickout = new TeamPanelBtn("skin/8100226/newbutton/B22.png", 1, "请离队伍",
					UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, this, null);
			btnKickout.setBounds(293, 232, 99, 24);
			this.add(btnKickout);
			// 加为好友
			btnfiend = new TeamPanelBtn("skin/8100226/newbutton/B22.png", 1, "加为好友",
					UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, this, null);
			btnfiend.setBounds(413, 232, 99, 24);
			this.add(btnfiend);
		}else{

			this.setPreferredSize(new Dimension(515, 306));
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);
			modelJpanels = new TeamModelJpanel[5];
			operates = new TeamPanelBtn[4];
			for (int i = 0; i < modelJpanels.length; i++) {
				modelJpanels[i] = new TeamModelJpanel(this, i);
				modelJpanels[i].setBounds(29 + i * 95, 48+15, 84, 174);
				this.add(modelJpanels[i]);
			}
			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 13);
			offBtn.setBounds(515 - 25, 0, 23, 23);
			this.add(offBtn);
			// 设为队长
			btnTobecaptain = new TeamPanelBtn("inkImg/hongmu/1/B22h.png", 1, "移交队长",
					UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, this, null);
			btnTobecaptain.setBounds(385-20, 20+13, 99, 26);
			this.add(btnTobecaptain);
			// 申请列表
			btnApply = new TeamPanelBtn("inkImg/hongmu/1/B22h.png", 1, "申请列表",
					UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, this, null);
			btnApply.setBounds(53-23, 232+15, 99, 26);
			this.add(btnApply);
			// 解散队伍
			btnOut = new TeamPanelBtn("inkImg/hongmu/1/B22h.png", 1, "解散队伍",
					UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, this, null);
			btnOut.setBounds(173-23, 232+15, 99, 26);
			this.add(btnOut);
			// 踢出队伍
			btnKickout = new TeamPanelBtn("inkImg/hongmu/1/B22h.png", 1, "请离队伍",
					UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, this, null);
			btnKickout.setBounds(293-23, 232+15, 99, 26);
			this.add(btnKickout);
			// 加为好友
			btnfiend = new TeamPanelBtn("inkImg/hongmu/1/B22h.png", 1, "加为好友",
					UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, this, null);
			btnfiend.setBounds(413-23, 232+15, 99, 26);
			this.add(btnfiend);
		}
	}

	public void paintPeople(Graphics g) {
		if("水墨UI".equals(MyIsif.getStyle())) {

			for (int i = 0; i < 5; i++) {
				if (modelJpanels[i].getTeamRole() != null) {
					if (i == xz || i == sxz) {// 绘制选中
						if (icon2 == null) {
                            icon2 = new ImageIcon("inkImg/old/background/S163.png");
                        }
						g.drawImage(icon2.getImage(), modelJpanels[i].getX(),modelJpanels[i].getY(), this);
					}
					if (modelJpanels[i].getPart()!=null) {
						modelJpanels[i].getPart().draw(g, 90 + i * 95, 160, 4, ImageMixDeal.userimg.getTime());
					}
					if (i == 0) {
						Sprite mouse = SpriteFactory.Prepare(GetTcpPath.LIN);
						if (mouse != null) {
							mouse.updateToTime(ImageMixDeal.userimg.getTime(),0);
							mouse.draw(g, 80, 25);
						}
					}
				}
			}
		}else{

			for (int i = 0; i < 5; i++) {
				if (modelJpanels[i].getTeamRole() != null) {
					if (i == xz || i == sxz) {// 绘制选中
						if (icon2 == null) {
                            icon2 = new ImageIcon("inkImg/hongmu/1/S28952.png");
                        }
						g.drawImage(icon2.getImage(), modelJpanels[i].getX(),modelJpanels[i].getY(), this);
					}
					if (modelJpanels[i].getPart()!=null) {
						modelJpanels[i].getPart().draw(g, 70 + i * 95, 173, 4, ImageMixDeal.userimg.getTime());
					}
					if (i == 0) {
						Sprite mouse = SpriteFactory.Prepare(GetTcpPath.LIN);
						if (mouse != null) {
							mouse.updateToTime(ImageMixDeal.userimg.getTime(),0);
							mouse.draw(g, 80, 25);
						}
					}
				}
			}
		}
	}

	private ImageIcon icon_sm = new ImageIcon("inkImg/newbackground/B228.png");

	private ImageIcon icon_hm = new ImageIcon("inkImg/hongmu/1/S28955.png");
	private ImageIcon icon2;

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		// 组队
//		if (icon == null)
		if("水墨UI".equals(MyIsif.getStyle())) {
			g.drawImage(icon_sm.getImage(), 0, 0, 538, 279, this);
			paintPeople(g);
		}else{
			g.drawImage(icon_hm.getImage(), 0, 0, 515, 306, this);
			paintPeople(g);
		}
	}

	public void upSXz(int v) {
		this.sxz = v;
	}

	public void upXz(int v) {
		this.xz = v;
	}

	/** 展示 */
	public void show(RoleShow roleShow, TeamBean teamBean) {

			boolean isCaptian=teamBean.getTeams().get(0).getRoleId().compareTo(roleShow.getRole_id())==0;
	    	btnApply.setVisible(isCaptian);
			btnOut.setVisible(isCaptian);
			btnKickout.setText(isCaptian?"请离队伍":"离开队伍");
			for (int i = 0, length = teamBean.getTeams().size(); i < modelJpanels.length; i++) {
				TeamModelJpanel jpanel = modelJpanels[i];
				TeamRole teamRole = i < length ? teamBean.getTeams().get(i) : null;
				jpanel.showRoleShow(teamRole);
				if (i!=0) {
					if (isCaptian && teamRole != null && teamRole.getState() < 0) {
						if (operates[i-1]==null) {
							if("水墨UI".equals(MyIsif.getStyle())) {
								operates[i-1]=new TeamPanelBtn("skin/8100226/newbutton/B30.png",1,i,UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT);
								operates[i-1].setText("召回");
								operates[i-1].setBounds(97 + i * 95, 51, 34, 17);
								this.add(operates[i-1],0);
							}else{
								operates[i-1]=new TeamPanelBtn("inkImg/hongmu/1/B30h.png",1,i,UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_FONT);
								operates[i-1].setText("召回");
								operates[i-1].setBounds(73 + i * 95, 51+14, 34, 17);
								this.add(operates[i-1],0);
							}
						}
						operates[i-1].setVisible(true);
					} else {
						if (operates[i-1] != null) {
							operates[i-1].setVisible(false);
						}
					}
				}
				if (teamRole!=null&&teamRole.getRoleId().compareTo(roleShow.getRole_id())==0) {
					btnTobecaptain.setText(isCaptian?"移交队长":teamRole.getState()>=0?"暂离队伍":"回归队伍");
				}
			}
			upSXz(-1);
			upXz(-1);
			FormsManagement.showForm(13);
	}
	/** 获取选中的玩家 */
	public TeamRole getXZ() {
		if (xz == -1) {
			return null;
		}
		return modelJpanels[xz].getTeamRole();
	}
}
